package com.tracking.locon;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.TextView;

public class Contact extends Fragment implements GridView.OnScrollListener{
//public class Contact extends Activity implements ListView.OnScrollListener{
    
	//Contact Data
	private String[] mContactData;
	private GridView mContactGridView;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.contact, container, false);
        setupContact(view);
        return view;
    }
	
	private void setupContact(View view){
		mContactGridView = (GridView) view.findViewById(R.id.gvContact);
        mContactData = getContactData();
        setContactGridView(mContactGridView, mContactData);
        setContactScroll(mContactGridView);
	}
	
	/*
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact);
        mContactGridView = (GridView) findViewById(R.id.gvContact);
        mContactData = getContactData();
        setContactGridView(mContactGridView, mContactData);
        setContactScroll(mContactGridView);
	}
	*/
	private String[] getContactData(){
		return getResources().getStringArray(R.array.arrayTestContact);
	}
	
	private void setContactGridView(GridView gridView, String[] contactData){
		ContactOne[] contactList = new ContactOne[contactData.length];
        for(int i = 0; i < contactData.length; i++){
        	contactList[i] = new ContactOne(R.drawable.icon_person, contactData[i], 
        			Math.random() > 0.5 ? "Available" : "Busy");
        }
        Context context = getActivity();
        ContactAdapter adapter = new ContactAdapter(context, R.layout.contact_row, contactList);
        gridView.setAdapter(adapter);
	}
	/*
	private void setContactListView(ListView listView, String[] contactData){   
        ContactOne[] contactList = new ContactOne[contactData.length];
        for(int i = 0; i < contactData.length; i++){
        	contactList[i] = new ContactOne(R.drawable.icon_person, contactData[i]);
        }
        ContactAdapter adapter = new ContactAdapter(this, R.layout.contact_row, contactList);
        
        listView.setAdapter(adapter);
	}
	*/
	
	//Contact Scroll
	private RemoveWindow mRemoveWindow = new RemoveWindow();
    Handler mHandler = new Handler();
    private WindowManager mWindowManager;
    private TextView mDialogText;
    private boolean mShowing;
    private boolean mReady;
    private char mPrevLetter = Character.MIN_VALUE;
	
    private final class RemoveWindow implements Runnable {
        public void run() {
            removeWindow();
        }
    }
	
	private void setContactScroll(GridView gridView){
		
		Context context = getActivity();
 		mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
 		
 		gridView.setOnScrollListener(this);
 		
        LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
        mDialogText = (TextView) inflate.inflate(R.layout.contact_scroll, null);
        
        mDialogText.setVisibility(View.INVISIBLE);
        mHandler.post(new Runnable() {

            public void run() {
                mReady = true;
                WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_APPLICATION,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSLUCENT);
                mWindowManager.addView(mDialogText, lp);
            }});
	}
    
	@Override
	public void onResume() {
        super.onResume();
        mReady = true;
    }

    @Override
	public void onPause() {
        super.onPause();
        removeWindow();
        mReady = false;
    }

	@Override
	public void onDestroy() {
        super.onDestroy();
        mWindowManager.removeView(mDialogText);
        mReady = false;
    }

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}
	
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //int lastItem = firstVisibleItem + visibleItemCount - 1;
        if (mReady) {
            char firstLetter = mContactData[firstVisibleItem].charAt(0);
            if (!mShowing && firstLetter != mPrevLetter) {
            	
                mShowing = true;
                mDialogText.setVisibility(View.VISIBLE);
               
            }
            mDialogText.setText(((Character)firstLetter).toString());
            mHandler.removeCallbacks(mRemoveWindow);
            mHandler.postDelayed(mRemoveWindow, 500);
            mPrevLetter = firstLetter;
        }
    }
	
	private void removeWindow() {
        if (mShowing) {
            mShowing = false;
            mDialogText.setVisibility(View.INVISIBLE);
        }
    }
}
